/**
 * 
 */
package jMiiEditor.mii.checker.impl;

import java.io.IOException;
import java.io.OutputStream;
import net.sf.vfsjfilechooser.utils.VFSUtils;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.provider.AbstractFileObject;
import org.apache.commons.vfs.util.FileObjectUtils;
import jMiiEditor.mii.checker.MiiCheckerActionException;
import jMiiEditor.mii.checker.MiiCheckerFileObject;

/**
 * @author CC
 *
 */
public class MiiCheckerFileObjectVFSImpl 
    implements MiiCheckerFileObject 
{
    private FileObject file;
    
    public MiiCheckerFileObjectVFSImpl( FileObject vfsFileObject )
    {
        this.file = vfsFileObject;
    }
    
    @Override
    public String getDisplayPath()
    {
        return VFSUtils.getFriendlyName(file.toString());
    }

    @Override
    public Object getInternalObject()
    {
        return file;
    }

    @Override
    public OutputStream getOutputStream() throws IOException
    {
        AbstractFileObject abstractFileObject = FileObjectUtils.getAbstractFileObject( file );
        
        return abstractFileObject.getOutputStream();
    }
    
    @Override
    public MiiCheckerFileObject renameTo( String newFileName ) throws MiiCheckerActionException
    {
        FileObject  parent  = null;
        FileObject  foNew   = null;
        
        try {
            parent = file.getParent();
        }
        catch( FileSystemException e ) {
            throw buildMiiCheckerActionException(
                    "Can't get parent",
                    newFileName,
                    foNew,
                    e
                    );
        }

        if( parent == null ) {
            throw buildMiiCheckerActionException(
                    "Can't find parent",
                    newFileName,
                    foNew,
                    null
                    );
        }
        
        try {
            foNew = parent.resolveFile( newFileName );
        }
        catch( FileSystemException e ) {
            throw buildMiiCheckerActionException(
                    "Can't get create new FileObject",
                    newFileName,
                    foNew,
                    e
                    );
        }

        if( foNew != null ) {
            try {
                file.moveTo( foNew );
            }
            catch( FileSystemException e ) {
                throw buildMiiCheckerActionException(
                        null,
                        newFileName,
                        foNew,
                        e
                        );
            } 
        }
        else {
            throw buildMiiCheckerActionException(
                    null,
                    newFileName,
                    foNew,
                    null
                    );
        }
        
        return new MiiCheckerFileObjectVFSImpl( foNew );
    }
    
    private MiiCheckerActionException buildMiiCheckerActionException(
            String              message,
            String              newFileName,
            FileObject          newFileObject,
            FileSystemException cause
            )
    {
        String newFileNameString;
        
        if( newFileObject != null ) {
            newFileNameString = VFSUtils.getFriendlyName(newFileObject.toString());
        }
        else {
            newFileNameString = newFileName;
        }
        
        StringBuilder sb = new StringBuilder();
        
        sb.append( "Can't rename [" );
        sb.append( VFSUtils.getFriendlyName(file.toString()) );
        sb.append( "] to [" );
        sb.append( newFileNameString );
        sb.append( ']' );
        
        if( message != null ) {
            sb.append( ' ' );
            sb.append( message );
        }

        return new MiiCheckerActionException(sb.toString(), cause);
    }

}
